package runnable_functions;

import gui.Main;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import details.Comment;
import details.Track;

public class EDITfunctions extends RunnableClasses{
	
	public EDITfunctions(){}
	
	public class Edit implements Runnable{

		private String identifier;
		private ReferedObject refObj;
		
		private String changed_name;
		private CountDownLatch startSignal;
		
		private String userName;
		
		public Edit(String identifier,ReferedObject refObj,String changed_name,String userName,CountDownLatch startSignal){
			this.identifier = identifier;
			this.refObj = refObj;
			this.changed_name = changed_name;
			this.startSignal = startSignal;
			
			
			this.userName = userName;
		}
		
		@Override
		public void run() {
			try{
				if(checkIfAdmin(userName)){
					Main.db.openConnection();
					switch(refObj)
					{
					case SONG:
						/* i don't check if changed_name is null or not */
						Main.db.edit(Integer.parseInt(identifier),refObj,changed_name);
						
						break;
					case ARTIST:
						Main.db.edit(Integer.parseInt(identifier),refObj,changed_name);
						
						break;
					case GENRE:
						Main.db.editGenre(identifier, changed_name);
						
						break;
					case LYRIC:
						Main.db.edit(Integer.parseInt(identifier),refObj,changed_name);
						
						break;
					
						
					}
					Main.guiInt.notifySuccess();
				}
				else {
					Main.guiInt.notifyGeneralError("You don't have authority to edit!");
				} 
				
			
			}
			catch(SQLException e){
				Main.guiInt.notifyDBFailure(e.getMessage());
			} catch (NumberFormatException e) {
				Main.guiInt.notifyGeneralError(e.getMessage());
			} catch (Exception e) {
				Main.guiInt.notifyGeneralError(e.getMessage());
			}
			finally{
				Main.db.closeConnection();
				startSignal.countDown();
				
			}
			
		}
		
	}
	
	public class EditCd implements Runnable{
		private String cd_id;
		private String changed_name;
		private int changed_cdYear;
		private String changed_performer;
		private int changed_length;
		private ArrayList<Track> changedCdTracks;
		private String userName;
		private int[] trackLength;
		private CountDownLatch startSignal;
		
		
		public EditCd(String cd_id,String changed_name,int changed_cdYear,String changed_performer,ArrayList<Track> changedCdTracks,String userName,CountDownLatch startSignal){
			this.cd_id = cd_id;
			this.changed_cdYear = changed_cdYear;
			this.trackLength = new int[changedCdTracks.size()];
			//System.out.println(changedCdTracks.size());
			this.changedCdTracks = changedCdTracks;
			this.changed_length = computeCdLength(trackLength);
			this.changed_name = changed_name;
			this.changed_performer = changed_performer;
			for (int i = 0; i < changedCdTracks.size(); i++) {
				this.trackLength[i] = changedCdTracks.get(i).getLengthInSec();
			}
			
			
			this.userName = userName;
			this.startSignal = startSignal;
			
		}

		@Override
		public void run() {
			try {
				if(checkIfAdmin(userName)){
				Main.db.openConnection();
				
				System.out.println("calling editcd in db");
				Main.db.editCD(cd_id,changed_name, changed_cdYear, changed_performer, changed_length);
				if(changedCdTracks!=null && !changedCdTracks.isEmpty() /************************************/){
					System.out.println("calling deleting songs");
					Main.db.deleteAllTracks(cd_id);
					System.out.println("calling adding songs");
					Main.db.addTracksToDisc(changedCdTracks);
					System.out.println("out od db - success");
					
				}
				}
				else {
					Main.guiInt.notifyGeneralError("You don't have authority to edit!");
				}
				
			} catch (SQLException e) {
				Main.guiInt.notifyDBFailure(e.getMessage());
			} catch (IllegalStateException e) {
				Main.guiInt.notifyGeneralError(e.getMessage());
			}
			finally{
				Main.db.closeConnection();
				startSignal.countDown();
			}
			
			
		}
		
		private  int computeCdLength(int trackLen[]){
			int len = 2;/* in sec*/
			for(int trackLength : trackLen){
				len += trackLength;
			}
			
			return len;
		}
		
	}
	
	public class EditComment implements Runnable{

		private Comment comment;
		private String new_comment;
		private String userName;
		private String new_DATETIME;
		public EditComment(Comment comment,String new_comment,String new_DATETIME,String userName){
			this.comment = comment;
			this.new_comment = new_comment;
			this.new_DATETIME = new_DATETIME;
			this.userName = userName;
		}
		
		
		@Override
		public void run() {
			try{
				
				if(userName.equals(comment.getUser().getUserName())){
						Main.db.openConnection();
						Main.db.editSongComment(comment.getIdNum(),new_DATETIME,new_comment);
						
						Main.db.closeConnection();
					}
				else {
					Main.guiInt.notifyGeneralError("you dont have authority to edit this comment");
				}
			}
			catch(SQLException e){
				Main.guiInt.notifyDBFailure(e.toString());
				
				Main.db.closeConnection();
			}
		}
	}

}
